<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <title>查询</title>
  <link rel="stylesheet" href="css/layui.css" media="all">
  <style>
    body{margin: 10px;}
    .demo-carousel{height: 200px; line-height: 200px; text-align: center;}
  </style>
</head>
<body>
<table class="layui-hide" id="demo" lay-filter="test"></table>
 
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="lay/jquery.min.js"></script>
<script src="layui.js"></script>
<script>

layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider'], function(){
  var laydate = layui.laydate //日期
  ,laypage = layui.laypage //分页
  ,layer = layui.layer //弹层
  ,table = layui.table //表格
  ,carousel = layui.carousel //轮播
  ,upload = layui.upload //上传
  ,element = layui.element //元素操作
  ,slider = layui.slider //滑块
 

  
  //监听Tab切换
  element.on('tab(demo)', function(data){
    layer.tips('切换了 '+ data.index +'：'+ this.innerHTML, this, {
      tips: 1
    });
  });
  //执行一个 table 实例
  table.render({
    elem: '#demo'
    ,height: 650
    ,url: 'department?opr=query' //数据接口
    ,title: '用户表'
    ,page: true //开启分页
    ,toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
    ,cols: [[ //表头
       {type: 'checkbox', fixed: 'left'}
      ,{field:'id',title: 'id', width:330, title: '员工编号', sort: true}
      ,{field:'dname', width:330, title: '部门名', sort: true}
      ,{field:'dloc', width:330, title: '部门地址', sort: true}
      ,{field:'cz',width:300, title: '操作', toolbar:'#barDemo'}
    ]]
  });
//监听头工具栏事件
  table.on('toolbar(test)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id)
    ,data = checkStatus.data; //获取选中的数据
    switch(obj.event){
      case 'add':
    	  layer.open({
			  title: '添加',
			  type: 2,
			  area: ["1000px","500px"],
			  shadeClose: false,
			  content: 'SectionAdd.html'
			});
      break;
      case 'update':
        if(data.length === 0){
          layer.msg('请选择一行');
        } else if(data.length > 1){
          layer.msg('只能同时编辑一个');
        } else {
        	layer.msg('编辑 [id]：'+ checkStatus.data[0].id);
        }
      break;
      case 'delete':
        if(data.length === 0){
          layer.msg('请选择一行');
        } else {
          layer.msg('删除');
        }
      break;
    };
  });
  
  table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
	    var data = obj.data //获得当前行数据
	    ,layEvent = obj.event; //获得 lay-event 对应的值
	     var tr = obj.tr; 
	    if(layEvent === 'detail'){
	    	 layer.open({
	               type:2
	               ,area: ["1000px","500px"]
	               ,content:'SectionExamine.html?id='+data.id
	               ,title:'查看用户'
	            })
	    } else if(layEvent === 'del'){
	    	layer.confirm('真的删除行么', function(index){
	        obj.del(); //删除对应行（tr）的DOM结构
	        layer.close(index);
	        //向服务端发送删除指令，获取后端
            $.ajax({
                type: 'get',
                url: "department?opr=delete",
                data: {
                    id: data.id,  //此处是根据id查询此id是否被删除，故将id传向后端
                },
                contentType: 'application/json',
                success: function (data) {
                	layer.msg("删除成功!")
                },
                error: function (data) {
                	layer.msg('连接网络失败，请检查网络设置或联系管理员', { icon: 2 }, { time: 2000 });
                }
            });
	      });
	    }else if(layEvent === 'edit'){
	    	  layer.open({
	               type:2
	               ,area: ["1000px","500px"]
	               ,content:'SectionUpdate.html?id='+data.id
	               ,title:'查看用户'
	            })
	        } else {
	        	layer.msg("先选择行")
	        }
	    
    });

}); 
</script>
</body>
</html>        
        